//
//  GameServerViewController.m
//  YoucApp
//
//  Created by Liam on 12-10-3.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "GameServerViewController.h"
#import "ListManager.h"
#import "AllListViewController.h"
#import "RecommendListViewController.h"
#import "AttentionListViewController.h"

@interface GameServerViewController()

@end

@implementation GameServerViewController

@synthesize segmentControl = _segmentControl;
@synthesize listViewControllers = _listViewControllers;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.delegate = self;
    
    NSMutableArray *array = [NSMutableArray array];
    GameListViewController *listViewController = [[[AllListViewController alloc] init] autorelease];
    [array addObject:listViewController];
    
    listViewController = [[[RecommendListViewController alloc] init] autorelease];
    [array addObject:listViewController];
    
    listViewController = [[[AttentionListViewController alloc] init] autorelease];
    [array addObject:listViewController];
    
    self.listViewControllers = array;
    
    [self setViewControllers:[NSArray arrayWithObject:[self.listViewControllers objectAtIndex:0]]];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
	
	CGRect frame = self.segmentControl.frame;
	frame.origin.y = CGRectGetMaxY(self.navigationBar.frame);
	frame.size.height = 33;
	
	self.segmentControl.frame = frame;

    [self.view addSubview:self.segmentControl];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    self.segmentControl = nil;
    self.listViewControllers = nil;
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [_segmentControl release];
    [_listViewControllers release];
    [super dealloc];
}

- (IBAction)segValueChanged:(id)sender
{    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.segmentControl.selectedSegmentIndex + 1) ];
    
    NSArray *array = [self.listViewControllers objectsAtIndexes:indexSet];
    
    [self setViewControllers:array animated:YES];
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
	CGRect rect = self.segmentControl.frame;
	[UIView beginAnimations:@"segment" context:nil];
	[UIView setAnimationDuration:0.3];
	if ([self.listViewControllers indexOfObject:viewController] ==  NSNotFound)
	{
		rect.origin.x = -rect.size.width;
		self.segmentControl.frame = rect;
	}
	else
	{
		rect.origin.x = 0;
		self.segmentControl.frame = rect;
	}
	[UIView commitAnimations];
}

@end
